package com.eic.framework.spring;

import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.web.context.support.StandardServletEnvironment;

/**
 * 启动监听，设置启动环境变量
 * @author guochun
 */
public class AppStartingEventListener implements ApplicationListener<ApplicationStartingEvent>{
	@Override
	public void onApplicationEvent(ApplicationStartingEvent event) {
		ConfigurableEnvironment env=new StandardServletEnvironment();
		env.getPropertySources().addFirst(new SimpleCommandLinePropertySource(event.getArgs()));
		loggerLevel(env);
		event.getSpringApplication().setEnvironment(env);
		//spring.pid.file
		event.getSpringApplication().addListeners(new ApplicationPidFileWriter());
		if(env.getProperty("log.path")==null || env.getProperty("log.path").length()==0){
			env.getSystemProperties().put("log.path", "./logs");
		}
	}

	private void loggerLevel(ConfigurableEnvironment env) {
		//logger
		env.getSystemProperties().put("logging.level.org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver", "ERROR");
	}
}
